package com.seal.yoyoshop.adminweb;

import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "TypeAddServlet", value = "/admin/typeAdd")
public class TypeAddServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setAttribute("flag",4);
        List<Type> typeList = typeService.getTypeList();
        request.setAttribute("typeList",typeList);

        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");

        Type type = new Type();
        type.setName(name);
        boolean isSuccess = typeService.addType(type);
        if (isSuccess){
            request.setAttribute("msg","添加成功");
        }else request.setAttribute("msg","添加失败，大类名过长");

        response.sendRedirect("typeList");
    }
}
